package com.hex.ds.hdtp.core.app.script.common.service.impl;

import com.hex.ds.hdtp.core.app.script.common.converter.IScriptTableConsanguinityConverter;
import com.hex.ds.hdtp.core.app.script.common.dto.request.ScriptTableConsanguinityRequestDto;
import com.hex.ds.hdtp.core.app.script.common.dto.response.ScriptTableConsanguinityResponseDto;
import com.hex.ds.hdtp.core.app.script.common.service.IScriptTableConsanguinityService;
import com.hex.ds.hdtp.core.inf.script.common.po.ScriptTableConsanguinityPo;
import com.hex.ds.hdtp.core.inf.script.common.repository.IScriptTableConsanguinityRepository;
import com.hex.ds.hdtp.core.inf.script.test.po.ScriptDataTestListPo;
import com.hex.ds.hdtp.core.inf.script.test.repository.IScriptDataTestListRepository;
import com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTargetListPo;
import com.hex.ds.hdtp.core.inf.script.transfer.repository.IScriptConvertTargetListRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.app.script.test.service.impl
 * @ClassName ScriptTestConsanguinityService
 * @Description: 脚本表血缘-业务接口实现
 * @Author: wz.li
 * @Date 2023/8/1 16:32
 * @Version v2.0.2
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptTableConsanguinityService implements IScriptTableConsanguinityService {

    private final IScriptTableConsanguinityRepository scriptTestConsanguinityRepository;

    private final IScriptConvertTargetListRepository scriptConvertTargetListRepository;

    private final IScriptDataTestListRepository scriptDataTestListRepository;

    private final IScriptTableConsanguinityConverter converter;

    /*
     * @Method: queryList <br>
     * @Param: [scriptListPkId, dataFrom] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.app.script.common.dto.response.ScriptTableConsanguinityResponseDto> <br>
     * @Description：不分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/3 16:52 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<ScriptTableConsanguinityResponseDto> queryList(String scriptListPkId, String dataFrom) {
        final List<ScriptTableConsanguinityPo> scriptTableConsanguinityPos = scriptTestConsanguinityRepository.queryList(scriptListPkId, dataFrom);
        List<ScriptTableConsanguinityResponseDto> scriptTableConsanguinityResponseDtos = converter.toScriptTableConsanguinityResponseDtoList(scriptTableConsanguinityPos);
        // 查询脚本测试清单获取脚本转换列表的主键，如果该脚本还没有添加到脚本测试列表中，则直接返回该脚本的表血缘关系
        final ScriptDataTestListPo scriptDataTestListPo = scriptDataTestListRepository.queryById(scriptListPkId);
        if (scriptDataTestListPo != null) {
            String scriptConvertListPkId = scriptDataTestListPo.getScriptConvertListPkId();
            // 获取脚本转换后的数据源
            final ScriptConvertTargetListPo scriptConvertTargetListPo = scriptConvertTargetListRepository.queryById(scriptConvertListPkId);
            String ConvertSourceConnCode = scriptConvertTargetListPo.getSourceConnCode();
            String ConvertTargetConnCode = scriptConvertTargetListPo.getTargetConnCode();
            // 为脚本表血缘关系设置数据源
            for (ScriptTableConsanguinityResponseDto scriptTableConsanguinityResponseDto : scriptTableConsanguinityResponseDtos) {
                scriptTableConsanguinityResponseDto.setSourceCode(ConvertSourceConnCode);
                scriptTableConsanguinityResponseDto.setTargetCode(ConvertTargetConnCode);
            }
        }
        return scriptTableConsanguinityResponseDtos;
    }

    /**
     * @Method queryListByTaskId
     * @Param taskPkId
     * @Return java.util.List<com.hex.ds.hdtp.core.app.script.common.dto.response.ScriptTableConsanguinityResponseDto>
     * @Description 根据任务主键查询对应的表血缘
     * @Author gj.xu
     * @Date 2023/12/13 15:09
     * @Version V2.0
     */
    @Override
    public List<ScriptTableConsanguinityResponseDto> queryListByTaskId(String taskPkId) {
        return converter.toScriptTableConsanguinityResponseDtoList(scriptTestConsanguinityRepository.queryListByTaskId(taskPkId));
    }

    /*
     * @Method: add <br>
     * @Param: [scriptTestConsanguinityRequestDto] <br>
     * @Return: void <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/1 16:39 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void add(ScriptTableConsanguinityRequestDto scriptTableConsanguinityRequestDto) {
        int count = scriptTestConsanguinityRepository.add(converter.toScriptTestConsanguinityPo(scriptTableConsanguinityRequestDto));
        if (count != 1) {
            throw new RuntimeException("测试脚本血缘-新增失败");
        }
    }

    /*
     * @Method: removeByScriptListPkId <br>
     * @Param: [scriptListPkId, dataFrom] <br>
     * @Return: void <br>
     * @Description：根据脚本清单主键和数据来源删除对应的表血缘<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/4 11:59 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void removeByScriptListPkId(String scriptListPkId, String dataFrom) {
        scriptTestConsanguinityRepository.removeByScriptListPkId(scriptListPkId, dataFrom);
    }

    /*
     * @Method: removeByTableName <br>
     * @Param: [scriptTableConsanguinityRequestDto] <br>
     * @Return: void <br>
     * @Description：根据表名删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/8 11:44 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void removeByTableName(ScriptTableConsanguinityRequestDto scriptTableConsanguinityRequestDto) {
        scriptTestConsanguinityRepository.removeByTableName(converter.toScriptTestConsanguinityPo(scriptTableConsanguinityRequestDto));
    }
}
